﻿define(function (require) {
    $(function () {
        var service = require('ezdesk/utility/service');
        var utility = require('ezdesk/utility/utility.js');
        //var NotificationManager = require('ezdesk/service/notification-manager');

        require('model/Feed');
        require('model/FeedList');

        var DateView = require('view/DateView');
        var UpdateView = require('view/UpdateView');
        var NoticificationView = require('view/NotificationView');
        var FeedListView = require('view/FeedListView');

        var loadBegin = function () {
            $('#feeds').hide();
        }

        var loadEnd = function () {
            $('#feeds').show();
        }

        var refreshFeeds = function () {
            utility.AjaxLoad.AjaxLoadBegin();

            service.GetFeeds(function (feeds) {
                feeds = _.select(feeds, function (item) {
                    return item.IsAvailable && item.IsDisplayInMainPanel;
                })
                utility.AjaxLoad.AjaxLoadEnd();
                feedsView.model.reset(feeds);
                feedsView.render();
            });
        }


        $.receiveMessage(function (e) {
            var paramKeyRegex = /\w*(?=\:)/g;
            var key = paramKeyRegex.exec(e.data);
            key = key ? key[0] : e.data;

            if (key == 'refreshFeeds')
                refreshFeeds();
            if (key == 'show')
                refreshFeeds();

        });


        feedsView = new FeedListView({ model: new FeedList });

        feedsView.bind('empty', function () {
            $('.ui-remind').html('没有消息').show();
        });

        refreshFeeds();
    })
})
